package com.ysd.lis.service.lab.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.LabItemDict;
import com.ysd.lis.entity.lab.LabTemplateDetail;
import com.ysd.lis.mapper.lab.LabItemDictMapper;
import com.ysd.lis.mapper.lab.LabTemplateDetailMapper;
import com.ysd.lis.request.TemplateDetailDto;
import com.ysd.lis.request.lab.LabImgTempleteDto;
import com.ysd.lis.request.lab.LabRptresultTempDto;
import com.ysd.lis.service.lab.LabTemplateDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HuiXing
 * @since 2024-01-02
 */
@Service
public class LabTemplateDetailServiceImpl extends ServiceImpl<LabTemplateDetailMapper, LabTemplateDetail> implements LabTemplateDetailService {

    @Autowired
    LabTemplateDetailMapper labTemplateDetailMapper;
    @Autowired
    LabItemDictMapper labItemDictMapper;

    @Override
    public Result getItemByTemplate(String id) {
        LambdaQueryWrapper<LabTemplateDetail> queryWrapper =new LambdaQueryWrapper<>();

        queryWrapper.eq(LabTemplateDetail::getDelFlag,0).eq(LabTemplateDetail::getTemplateId,id)
                .orderByAsc(LabTemplateDetail::getSeq);
        List<LabTemplateDetail> labTemplateDetails = labTemplateDetailMapper.selectList(queryWrapper);
        return Result.succ(1,"查询成功",labTemplateDetails);
    }

    @Override
    public Result getItemNotTemplate(String id) {
        LambdaQueryWrapper<LabItemDict> queryWrapper=new LambdaQueryWrapper<>();

        queryWrapper.eq(LabItemDict::getDelFlag,0)
                .eq(LabItemDict::getRptGroup,id);

        List<LabItemDict> labItemDicts = labItemDictMapper.selectList(queryWrapper);

        return Result.succ(1,"查询成功",labItemDicts);

    }

    @Override
    public Result updateItemTemplate(LabTemplateDetail labTemplateDetail) {
        boolean save = this.save(labTemplateDetail);
        if (save){
            return Result.succ(1,"新增成功","");
        }else {
            return Result.fail("新增失败");
        }
    }

    /**
     * 批量删除
     * @param dto
     * @return
     */
    @Override
    public Result batchDeleteTemplateDetailList(TemplateDetailDto dto) {
        List<LabTemplateDetail> labTemplateDetailList = dto.getLabTemplateDetailList();
        List<String> ids = labTemplateDetailList.stream().map(LabTemplateDetail::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }

    @Override
    public Result updateTemplateItemInfo(LabTemplateDetail labTemplateDetail) {
        int i = labTemplateDetailMapper.updateById(labTemplateDetail);
        if (i>0){
            return Result.succ(1,"编辑成功","");
        }else{
            return Result.fail("编辑失败");
        }

    }

    @Override
    public Result batchUpdateTemplateItemInfo(LabImgTempleteDto labImgTempleteDto) {
        List<LabTemplateDetail> labTemplateDetailList = labImgTempleteDto.getLabTemplateDetailList();
        boolean b = this.saveOrUpdateBatch(labTemplateDetailList);
        if (b){
            return Result.succ(1,"编辑成功","");
        }else{
            return Result.fail("编辑失败");
        }
    }
}
